var loginUrl = 'http://127.0.0.1:8848/ERP-WEB/login.html';
//var loginUrl = 'http://42.194.192.60/login.html';  // 前端的路径

var baseUrl = 'http://192.168.142.140/'

var api = "http://127.0.0.1:8080/api/";
//var api = "http://42.194.192.60:8080/";

var token = $.cookie('TOKEN');

var username = localStorage.getItem("username");

var pers = localStorage.getItem("permissions");

var usertype = localStorage.getItem("usertype");



// 给页面显示登录名
$(".login-name").html(username);

// 设置全局ajax前置拦截
$.ajaxSetup({
	headers: {
		'TOKEN': token //每次ajax请求时把token带过去
	}
})

// 访问登录页面以外的页面，如果没有携带TOKEN，则跳转到登录页面
if (token == undefined) {
	if (window.location.href != loginUrl) {
		window.location.href = loginUrl
	}
} else {
	if (window.location.href != loginUrl) {
		$.ajax({
			url: api + "login/checkLogin",
			async: true,
			type: 'post',
			dataType: 'json',
			success: function(res) {
				if (res.code == -1) {
					window.location.href = loginUrl;
				}
			},
			error: function(res) {
				window.location.href = loginUrl;
			}
		});
	}
}

if (usertype == 1) {
	if (pers != null) {
		var permission = pers.split(",");
		// 部门
		if (permission.indexOf("dept:add") < 0) {
			$(".btn-add").hide();
		}
		if (permission.indexOf("dept:update") < 0) {
			$(".btn-update").hide();
		}
		if (permission.indexOf("dept:delete") < 0) {
			$(".btn-delete").hide();
		}

		//菜单
		if (permission.indexOf("menu:add") < 0) {
			$(".btn-add-menu").hide();
		}
		if (permission.indexOf("menu:update") < 0) {
			$(".btn-update-menu").hide();
		}
		if (permission.indexOf("menu:delete") < 0) {
			$(".btn-delete-menu").hide();
		}

		//角色
		if (permission.indexOf("role:add") < 0) {
			$(".btn-add-role").hide();
		}
		if (permission.indexOf("role:update") < 0) {
			$(".btn-update-role").hide();
		}
		if (permission.indexOf("role:delete") < 0) {
			$(".btn-delete-role").hide();
		}
		if (permission.indexOf("role:asign") < 0) {
			$(".btn-asign-role").hide();
		}

		//用户
		if (permission.indexOf("user:add") < 0) {
			$(".btn-add-user").hide();
		}
		if (permission.indexOf("user:update") < 0) {
			$(".btn-update-user").hide();
		}
		if (permission.indexOf("user:delete") < 0) {
			$(".btn-delete-user").hide();
		}
		if (permission.indexOf("user:asign") < 0) {
			$(".btn-asign-user").hide();
		}
		if (permission.indexOf("user:reset") < 0) {
			$(".btn-reset").hide();
		}

		//公告
		if (permission.indexOf("notice:add") < 0) {
			$(".btn-add-notice").hide();
		}
		if (permission.indexOf("notice:update") < 0) {
			$(".btn-update-notice").hide();
		}
		if (permission.indexOf("notice:delete") < 0) {
			$(".btn-delete-notice").hide();
		}
		if (permission.indexOf("notice:batchdelete") < 0) {
			$(".btn-batchdelete").hide();
		}

	} else {
		$(".btn-add").hide();
		$(".btn-update").hide();
		$(".btn-delete").hide();
		$(".btn-reset").hide();
		$(".btn-asign").hide();
		$(".btn-batchdelete").hide();
	}
}
